package com.yfeng.goodjuhome.action;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.yfeng.exception.ServiceException;
import com.yfeng.goodjuhome.model.User;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.yfeng.common.utils.BeanUtil;
import com.yfeng.goodjuhome.service.IUserService;

@Controller("loginAction")
public class LoginAction {

	private final Log log = LogFactory.getLog(this.getClass());

	@Autowired
	@Qualifier("userService")
	private IUserService userService;
	

    /**
     *
     * @param request
     * @param response
     * @param username
     * @param passwd
     * @param nextAction 是BLOG登录还是QY主页登录 0:blog  1:qyhome
     * @param validWord
     * @param model
     * @param session
     * @throws Exception
     */
	@RequestMapping("/home/tologin")
	public void homeLogin(HttpServletRequest request, HttpServletResponse response,
			String username, String passwd,String nextAction,String validWord, ModelMap model,HttpSession session)throws Exception{
		
		try {

			request.setCharacterEncoding("UTF-8");
			response.setContentType("text/html;charset=UTF-8");
			PrintWriter write = response.getWriter();
			
			String rand=session.getAttribute("rand").toString();
			if(!validWord.equals(rand)){
				write.print("<script>alert('验证码错误!'); history.back();</script>");
			}
			            
			boolean ret = userService.toLogin(username, passwd);
			if(ret){
                String url = request.getRequestURL().toString();                 
				User user= userService.getUserByName(username);
				request.getSession().setAttribute("currUser", user);
				write.print("<script>alert('登陆成功!');window.location='"+ request.getContextPath()+ "/"+nextAction+"';</script>");
			}else {
				write.print("<script>alert('登陆失败!');history.back();</script>");
			}
			
		} catch (Exception e) {
			log.error(e.getMessage(),e);
            throw new ServiceException(e.getMessage(),e);
		}

	}
	


	/*
	 * 用户注销
	 */
	@RequestMapping("/home/tologout")
	public void logout(HttpServletRequest request,
			HttpServletResponse response, HttpSession session) {

		try {
			session.removeAttribute("currUser");
			
			request.setCharacterEncoding("UTF-8");
			response.setContentType("text/html;charset=UTF-8");
			PrintWriter write = response.getWriter();
              write.print("<script>window.location='/index.do';</script>");
			//int ret= userService.logout(user);
			

		} catch (Exception e) {
			// TODO Auto-generated catch block
			log.error(e.getMessage(),e);
		}

	}

}
